//
//  HMNavigationViewController.swift
//  Black22
//
//  Created by Apple on 16/11/18.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
//  自定义导航视图控制器
class HMNavigationViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  设置边缘手势代理
        self.interactivePopGestureRecognizer?.delegate = self
        
    }

    //  重写push方法，监听push操作
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //  隐藏底部tabbar
        if viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        
        
        
        super.pushViewController(viewController, animated: true)
        
        //  表示不是根视图控制器
        if viewControllers.count > 1 {
            
            if viewControllers.count == 2 {
                //  获取根视图控制器的标题
                let title = viewControllers.first!.title!
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
                viewController.title = "当前显示的第\(viewControllers.count)级控制器"
                
            } else {
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
                viewController.title = "当前显示的第\(viewControllers.count)级控制器"
            }
        }
        
    }

    // MARK:    --点击按钮
    @objc private func backAction() {
        popViewController(animated: true)
    }

    //  是否处理开始点击的边缘手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //  根视图控制器不处理边缘手势
        if viewControllers.count == 1 {
            return false
        }
        return true
    }
    
}





